package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.basic.WmsCodeRuleDetail;
import com.canaan.business.mapper.basic.WmsCodeRuleDetailMapper;
import com.canaan.business.service.basic.IWmsCodeRuleDetailService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.canaan.common.utils.SecurityUtils.getUsername;

/**
 * 条码规则明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-29
 */
@Service
public class WmsCodeRuleDetailServiceImpl implements IWmsCodeRuleDetailService {
    @Resource
    private WmsCodeRuleDetailMapper wmsCodeRuleDetailMapper;

    /**
     * 查询条码规则明细
     *
     * @param id 条码规则明细主键
     * @return 条码规则明细
     */
    @Override
    public WmsCodeRuleDetail selectWmsCodeRuleDetailById(Long id) {
        return wmsCodeRuleDetailMapper.selectWmsCodeRuleDetailById(id);
    }

    /**
     * 查询条码规则明细列表
     *
     * @param wmsCodeRuleDetail 条码规则明细
     * @return 条码规则明细
     */
    @Override
    public List<WmsCodeRuleDetail> selectWmsCodeRuleDetailList(WmsCodeRuleDetail wmsCodeRuleDetail) {
        return wmsCodeRuleDetailMapper.selectWmsCodeRuleDetailList(wmsCodeRuleDetail);
    }

    /**
     * 新增条码规则明细
     *
     * @param wmsCodeRuleDetail 条码规则明细
     * @return 结果
     */
    @Override
    public int insertWmsCodeRuleDetail(WmsCodeRuleDetail wmsCodeRuleDetail) {
        wmsCodeRuleDetail.setCreateTime(DateUtils.getNowDate());
        wmsCodeRuleDetail.setCreateBy(getUsername());
        return wmsCodeRuleDetailMapper.insertWmsCodeRuleDetail(wmsCodeRuleDetail);
    }

    /**
     * 修改条码规则明细
     *
     * @param wmsCodeRuleDetail 条码规则明细
     * @return 结果
     */
    @Override
    public int updateWmsCodeRuleDetail(WmsCodeRuleDetail wmsCodeRuleDetail) {
        wmsCodeRuleDetail.setUpdateTime(DateUtils.getNowDate());
        wmsCodeRuleDetail.setUpdateBy(getUsername());
        return wmsCodeRuleDetailMapper.updateWmsCodeRuleDetail(wmsCodeRuleDetail);
    }

    /**
     * 批量删除条码规则明细
     *
     * @param ids 需要删除的条码规则明细主键
     * @return 结果
     */
    @Override
    public int deleteWmsCodeRuleDetailByIds(Long[] ids) {
        return wmsCodeRuleDetailMapper.deleteWmsCodeRuleDetailByIds(ids);
    }

    /**
     * 删除条码规则明细信息
     *
     * @param id 条码规则明细主键
     * @return 结果
     */
    @Override
    public int deleteWmsCodeRuleDetailById(Long id) {
        return wmsCodeRuleDetailMapper.deleteWmsCodeRuleDetailById(id);
    }

    @Override
    public List<WmsCodeRuleDetail> selectWmsCodeRuleDetailListByRuleNo(String ruleNo) {
        WmsCodeRuleDetail param = new WmsCodeRuleDetail();
        param.setRuleNo(ruleNo);
        List<WmsCodeRuleDetail> wmsCodeRuleDetails = wmsCodeRuleDetailMapper.selectWmsCodeRuleDetailList(param);
        return wmsCodeRuleDetails;
    }

    @Override
    public void batchInsertCodeRuleDetail(List<WmsCodeRuleDetail> addWmsCodeRuleDetail) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (WmsCodeRuleDetail codeRuleDetail : addWmsCodeRuleDetail){
            if(StringUtils.isBlank(codeRuleDetail.getNo())){
                codeRuleDetail.setNo(TableNoUtil.getNo(WmsCodeRuleDetail.class));
            }
            codeRuleDetail.setCreateBy(username);
            codeRuleDetail.setCreateTime(currerTime);
        }
        wmsCodeRuleDetailMapper.batchInsertCodeRuleDetail(addWmsCodeRuleDetail);
    }

    @Override
    public void batchUpdateCodeRuleDetail(List<WmsCodeRuleDetail> updateWmsCodeRuleDetail) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (WmsCodeRuleDetail wmsCodeRuleDetail : updateWmsCodeRuleDetail) {
            wmsCodeRuleDetail.setUpdateBy(username);
            wmsCodeRuleDetail.setUpdateTime(currerTime);
        }
        wmsCodeRuleDetailMapper.batchUpdateCodeRuleDetail(updateWmsCodeRuleDetail);
    }
}
